package com.movie.controller;

import com.movie.entity.Review;
import com.movie.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/reviews") // 管理员接口基础路径
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
@PreAuthorize("hasRole('ADMIN')") // 类级别权限控制
public class AdminReviewController {

    @Autowired
    private ReviewService reviewService;

    // 管理员获取所有评论 (移除或保留作为非分页选项)
    // @GetMapping
    // public ResponseEntity<List<Review>> getAllReviews() {
    //     ...
    // }

    // 新增：管理员获取分页评论列表，支持筛选
    @GetMapping("/paged")
    public ResponseEntity<Map<String, Object>> getReviewsPaged(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String movieTitle, // 添加电影标题筛选
            @RequestParam(required = false) String username,   // 添加用户名筛选
            @RequestParam(required = false) Integer rating) {   // 添加评分筛选

        // 将筛选条件传递给 Service 层
        Map<String, Object> result = reviewService.getAllReviewsPaged(page, size, movieTitle, username, rating);
        return ResponseEntity.ok(result);
    }

    // 管理员获取特定电影的评论
    @GetMapping("/movie/{movieId}")
    public ResponseEntity<List<Review>> getReviewsByMovie(@PathVariable Long movieId) {
        List<Review> reviews = reviewService.getReviewsByMovieId(movieId);
        return ResponseEntity.ok(reviews);
    }

    // 管理员获取特定用户的评论
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Review>> getReviewsByUser(@PathVariable Long userId) {
        List<Review> reviews = reviewService.getReviewsByUserId(userId);
        return ResponseEntity.ok(reviews);
    }

    // 管理员删除评论
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteReview(@PathVariable Long id) {
        boolean deleted = reviewService.deleteReviewAsAdmin(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 管理员可能还需要更新评论的功能 (比如屏蔽敏感词)
    // @PutMapping("/{id}")
    // public ResponseEntity<Review> updateReview(@PathVariable Long id, @RequestBody Review review) {
    //     ...
    // }
} 